<template>
  <div class="app-container">
    <el-card>
      <!-- 搜索区域 -->
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px">
        <el-form-item label="关键词搜索" prop="keyword">
          <el-input
            v-model="queryParams.keyword"
            placeholder="手机号/姓名/宠物类型"
            clearable
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="实名状态" prop="authStatus">
          <el-select
            v-model="queryParams.authStatus"
            placeholder="全部状态"
            clearable
            style="width: 120px"
          >
            <el-option label="已认证" value="1" />
            <el-option label="未认证" value="0" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          <el-button
            type="warning"
            icon="el-icon-download"
            @click="handleExport"
          >导出Excel</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table
        v-loading="loading"
        :data="userList"
        border
        style="width: 100%"
      >
        <el-table-column label="用户ID" prop="userId" width="80" align="center" />
        <el-table-column label="姓名" prop="userName" width="100" align="center" />
        <el-table-column label="性别" prop="sex" width="80" align="center">
          <template slot-scope="scope">
            {{ scope.row.sex === '0' ? '男' : scope.row.sex === '1' ? '女' : '未知' }}
          </template>
        </el-table-column>
        <el-table-column label="身份证号" prop="idCard" width="180" align="center" />
        <el-table-column label="手机号" prop="phonenumber" width="120" align="center" />
        <el-table-column label="注册时间" prop="createTime" width="160" align="center">
          <template slot-scope="scope">
            {{ parseTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="实名状态" prop="authStatus" width="100" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.authStatus === '1' ? 'success' : 'danger'">
              {{ scope.row.authStatus === '1' ? '已认证' : '未认证' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="宠物类型" prop="petType" width="120" align="center" />
        <el-table-column label="历史订单" prop="orderCount" width="100" align="center">
          <template slot-scope="scope">
            {{ scope.row.orderCount }} 单
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { listUser, exportUser } from "@/api/system/user";
import { parseTime } from "@/utils/ruoyi";

export default {
  name: "UserData",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: undefined,
        authStatus: undefined
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('确认导出所有用户数据?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        return exportUser(queryParams);
      }).then(response => {
        this.download(response.msg);
      });
    }
  }
};
</script>
